﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.BusinessLayer;

public partial class Users : PageBaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UserRoleFactory urFactory = new UserRoleFactory();
            List<UserRole> roles = urFactory.GetAllBy(UserRole.UserRoleFields.Office_Code, _author.office_Code);

            ddlUserRole.DataSource = roles;
            ddlUserRole.DataTextField = UserRole.UserRoleFields.Role_Name.ToString();
            ddlUserRole.DataValueField = UserRole.UserRoleFields.Role_Code.ToString();
            ddlUserRole.DataBind();

            JobDescriptionFactory jFactory = new JobDescriptionFactory();
            List<JobDescription> jds = jFactory.GetAllBy(JobDescription.JobDescriptionFields.Office_Code, _author.office_Code);

            ddlJobDescription.DataSource = jds;
            ddlJobDescription.DataTextField = JobDescription.JobDescriptionFields.JD_Name.ToString();
            ddlJobDescription.DataValueField = JobDescription.JobDescriptionFields.JD_Code.ToString();
            ddlJobDescription.DataBind();

            DepartmentFactory dFactory = new DepartmentFactory();
            List<Department> deptList = dFactory.GetAllBy(Department.DepartmentFields.Office_Code, _author.office_Code);

            ddlDept.DataSource = deptList;
            ddlDept.DataTextField = Department.DepartmentFields.Department_Name.ToString();
            ddlDept.DataValueField = Department.DepartmentFields.Department_Code.ToString();
            ddlDept.DataBind();

            if (deptList.Count > 0)
            {
                ddlDept.SelectedIndex = 0;
                BindTheUsers(int.Parse(ddlDept.SelectedValue));
            }
            else
                BindTheUsers(0);

        }
    }

    private void BindTheUsers(int deptCode)
    {
        List<UserInfo> users;
        if (deptCode > 0)
        {
            users = new UserInfoFactory().GetAllBy(UserInfo.UserInfoFields.Department_Code, ddlDept.SelectedValue);
        }
        else
        {
            users = new UserInfoFactory().GetAll();
        }
        this.gvUsers.DataSource = users;
        this.gvUsers.DataBind();
    }

    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindTheUsers(int.Parse(ddlDept.SelectedValue));
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        UserInfo user = new UserInfo();
        user.User_Name = txtUserName.Text;
        user.User_Password = txtPassword.Text;
        user.FirstName = txtFirstName.Text;
        user.LastName = txtLastName.Text;
        user.Email_Address = txtEmail.Text;
        user.Sex = radMale.Checked ? radMale.Text : radFemale.Text;
        user.About_Me = txtAbout.Text;
        user.Activities = txtActivities.Text;
        user.Interests = txtInterest.Text;
        user.Street_Address = txtAddress.Text;
        user.City = txtCity.Text;
        user.Country = txtCountry.Text;
        user.ZIP_Code = txtZip.Text;

        user.Department_Code = int.Parse(ddlDept.SelectedValue);
        user.JD_Code = int.Parse(ddlJobDescription.SelectedValue);

        user.Online_Status = "Offline";

        user.Is_Approved = true;
        user.Approved_By = _author.user_Code;
        user.Created_By = _author.user_Code;

        if (new UserInfoFactory().Insert(user))
        {
            UserInRole uir = new UserInRole();
            uir.User_Code = user.User_Code;
            uir.Role_Code = int.Parse(ddlUserRole.SelectedValue);

            new UserInRoleFactory().Insert(uir);

            UserFolder defaultFolder = new UserFolder();
            defaultFolder.Folder_Name = "Uploaded";
            defaultFolder.Folder_Desc = "All uploaded documents are here.";
            defaultFolder.Is_Default = true;
            defaultFolder.User_Code = user.User_Code;

            new UserFolderFactory().Insert(defaultFolder);

            lblMessage.Text = "Saved Successfully";
        }
        else
        {
            lblMessage.Text = "Error!";
        }
        lblMessage.Visible = true;
    }
}
